package ch16 {
	import flash.display.Sprite;
	import flash.events.Event;
	import utils.Point3D;
	import utils.Triangle;

	/**
	 * 有些面似乎永远也看不可见。为什么会这样呢？立方体的背面总是被绘制出来。
	 * 同样，那两个三角形也根据它们在 triangles 数组中的位置进行绘制。因此在列表底部的面总是在列表顶部面之前被绘制出来，
	 * 您会感到奇怪，不可预知的效果就是这样形成的。我们需要调用 cull(剔除)，或铲掉、清除这些背面，因为它们不需要渲染。
	   我们会在下一章详细地讲解背面剔除，同时还要学到如何根据角度在每个面上应用一些基本的灯光。
	   作为本章的一个临时修正，我们可以进入到 Triangle 类的 draw 方法，设置填充的透明度为 0.5：
	   public function draw(g:Graphics):void {
	   g.beginFill(color, .5);
	   g.moveTo(pointA.screenX, pointA.screenY);
	   g.lineTo(pointB.screenX, pointB.screenY);
	   g.lineTo(pointC.screenX, pointC.screenY);
	   g.lineTo(pointA.screenX, pointA.screenY);
	   g.endFill();
	   }
	   这样就任何面都可以看到了，它使整个立方体像是用有色玻璃制成的。在学习背面剔除之前这只是个临时性的修正
	 * @author jianping.shenjp
	 */
	public class Cube extends Sprite {

		private var points:Array;
		private var triangles:Array;
		private var fl:Number = 250;
		private var vpX:Number = stage.stageWidth / 2;
		private var vpY:Number = stage.stageHeight / 2;

		public function Cube(){
			init();
		}

		private function init():void {
			points = new Array();
// 前面四个角
			points[0] = new Point3D(-100, -100, -100);
			points[1] = new Point3D(100, -100, -100);
			points[2] = new Point3D(100, 100, -100);
			points[3] = new Point3D(-100, 100, -100);
// 后面四个角
			points[4] = new Point3D(-100, -100, 100);
			points[5] = new Point3D(100, -100, 100);
			points[6] = new Point3D(100, 100, 100);
			points[7] = new Point3D(-100, 100, 100);
			for (var i:uint = 0; i < points.length; i++){
				points[i].setVanishingPoint(vpX, vpY);
				points[i].setCenter(0, 0, 200);
			}
			triangles = new Array();
			// front
			triangles[0] = new Triangle(points[0], points[1], points[2], 0x6666cc);
			triangles[1] = new Triangle(points[0], points[2], points[3], 0x6666cc);
// top
			triangles[2] = new Triangle(points[0], points[5], points[1], 0x66cc66);
			triangles[3] = new Triangle(points[0], points[4], points[5], 0x66cc66);
//back
			triangles[4] = new Triangle(points[4], points[6], points[5], 0xcc6666);
			triangles[5] = new Triangle(points[4], points[7], points[6], 0xcc6666);
// bottom
			triangles[6] = new Triangle(points[3], points[2], points[6], 0xcc66cc);
			triangles[7] = new Triangle(points[3], points[6], points[7], 0xcc66cc);
// right
			triangles[8] = new Triangle(points[1], points[5], points[6], 0x66cccc);
			triangles[9] = new Triangle(points[1], points[6], points[2], 0x66cccc);
// left
			triangles[10] = new Triangle(points[4], points[0], points[3], 0xcccc66);
			triangles[11] = new Triangle(points[4], points[3], points[7], 0xcccc66);
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function onEnterFrame(event:Event):void {
			var angleX:Number = (mouseY - vpY) * .001;
			var angleY:Number = (mouseX - vpX) * .001;
			for (var i:uint = 0; i < points.length; i++){
				var point:Point3D = points[i];
				point.rotateX(angleX);
				point.rotateY(angleY);
			}
			graphics.clear();
			for (i = 0; i < triangles.length; i++){
				triangles[i].draw(graphics);
			}
		}
	}
}